package com.thinkingcoder.tcgateway.plugin;

import com.thinkingcoder.tcgateway.plugin.chain.GatewayPluginChain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import com.thinkingcoder.tcgateway.properties.GatewayProperties;

import reactor.core.publisher.Mono;

/**
 * abstract gateway plugin.
 */
@Component
public abstract class AbstractGatewayPlugin implements GatewayPlugin {

    @Autowired
    private GatewayProperties properties;

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public String getPrefix() {
        return properties.getGatewayPrefix() + "/" + getContext() + "/";
    }

    @Override
    public boolean support(ServerWebExchange exchange) {
        return doSupport(exchange);
    }

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, GatewayPluginChain chain) {
        boolean supported = support(exchange);
        System.out.println(" ===>>> plugin [" + getName() + "], support = " + supported);
        return supported ? doHandle(exchange, chain) : chain.handle(exchange);
    }

    public abstract boolean doSupport(ServerWebExchange exchange);

    public abstract Mono<Void> doHandle(ServerWebExchange exchange, GatewayPluginChain chain);

}
